#pragma once
#include <iostream>
#include <string>
#include <cstring>
#include <memory>
#include <jsoncpp/json/json.h>

const std::string Sep = "\r\n";	
// 序列化后添加报头
bool AddHander(std::string &message)
{
	if(message.empty()) return false;
	std::string header = std::to_string(message.size()) + Sep + message +Sep;
	message = header;
	return true;
}

// 解析报头，并移除报头和内容
bool RemoveHander(std::string &package,std::string &content)
{
	if(package.empty()) return false;
	// 解析报头
	auto pos = package.find(Sep);
	std::string hedder = package.substr(0,pos);
	int content_size = std::stoi(hedder);
	int full_size = hedder.size() + content_size + 2*Sep.size();
	// 如果报头不完整，则不能读取，返回false
	if(package.size() < full_size) return false;
	// 读取内容
    content = package.substr(pos+Sep.size(),content_size);
	// 读取完毕移除内容+报头
	package.erase(0,full_size); // 消费数据
	return true;
}

class Requst
{
public:
	Requst():_x(0),_y(0),_oper(0)
	{
	}
	Requst(int x,int y,char oper):_x(x),_y(y),_oper(oper)
	{
	}
	bool Serialize(std::string &out_string)
	{
		//josn
		Json::Value root;
		root["x"] = _x;
		root["y"] = _y;
		root["oper"] = _oper;
		Json::StreamWriterBuilder builder;
		std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
		std::stringstream stream;
		writer->write(root, &stream);
		out_string = stream.str();
		return true;
	}
	bool Deserialize(std::string & in_string)
	{
		Json::Reader reader;
		Json::Value root;
		// 
		bool parsingSuccessful = reader.parse(in_string,root);
		if(!parsingSuccessful)
		{
			// 解析失败，输出错误信息
			std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
            return false;
		}
		_x = root["x"].asInt();
		_y = root["y"].asInt();
		_oper = root["oper"].asInt();
		return true;
	}
	void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _oper << std::endl;
        std::cout << _y << std::endl;
    }
	int X() const { return _x; }
    int Y() const { return _y; }
    char Oper() const { return _oper; }
private:
	int _x;
	int _y;
	char _oper;
};

class Response
{
public:
	Response():_result(0),_code(0)
	{}
	Response(int result,int code):_result(result),_code(code)
	{}
	bool Serialize(std::string &out_string)
	{
		Json::Value root;
		root["result"] = _result;
		root["code"] = _code;
		Json::StreamWriterBuilder bulider;
		std::unique_ptr<Json::StreamWriter> writer(bulider.newStreamWriter());
		std::stringstream stream;
		writer->write(root,&stream);
		out_string = stream.str();
		return true;
	}
	bool Deserialize(std::string &in_string)
	{
		Json::Reader reader;
		Json::Value root;
		bool parsingSuccessful = reader.parse(in_string,root);
		if(!parsingSuccessful)
		{
			std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
			return false;
		}
		_result = root["result"].asInt();
		_code = root["code"].asInt();
		return true;
	}
	int GetResult() const
	{
		return _result;
	}
	int GetCode() const
	{
		return _code;
	}
	void SetResult(int res) { _result = res; }
	void SetCode(int code) { _code = code; }
private:
	int _result;
	int _code; // 错误码
};